﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace SARD.Aux_Forms
{
    public partial class analise : Form
    {

        string path;
        string sfile;
        string[] text;
        bool newtest;
        string image;


        /// <summary>
        /// this function will store the acquisition information about the test.
        /// It will create a new text file to do it.
        /// </summary>
        /// <param name="dirpath">
        /// It's the directory address
        /// </param>
        /// <param name="newtest">
        /// It's a boolean variable identifying if this is a new test or the user is fixing the information about a previous one. 
        /// </param>
        public analise(string dirpath, string file,  bool newtest)
        {
            InitializeComponent();
            //The combo box will show the 2nd option, constancia, this option will be the most used one along all the tests.
            comboBox1.SelectedIndex = 1;
            //sets the path to a local variable that will be used by the program to do the desired functions.
            this.path = dirpath;
            image = file;

            if (newtest)
            {
                text = new string[5];
                this.newtest = true;
            }
            else
            {
                //Every directory has just a single txt file, which one has the informations about the test itself.
                string[] files = Directory.GetFiles(dirpath, "*.txt");
                sfile = files[0];

                text = File.ReadAllLines(sfile);
                this.newtest = false;

                //The informations inside the file will be shown to the user.
                dateTimePicker1.Text = text[0].Substring("#Teste:".Length + 1);
                textBox2.Text = text[1].Substring(label2.Text.Length + 1);
                textBox3.Text = text[2].Substring(label3.Text.Length + 1);
                textBox4.Text = text[3].Substring(label4.Text.Length + 1);
                comboBox1.SelectedItem = text[4].Substring(label5.Text.Length + 1);

            }

            
        }

        /// <summary>
        /// This is the continue button
        /// It will savev the informations inside the file.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {

            if (newtest)
            {
                //If new test there is no file or previous directory, both will be created with the data colected in the form.
                text[0] = "#Teste: " + dateTimePicker1.Text.ToString();
                text[1] = label2.Text.ToString() + " " + textBox2.Text.ToString();
                text[2] = label3.Text.ToString() + " " + textBox3.Text.ToString();
                text[3] = label4.Text.ToString() + " " + textBox4.Text.ToString();
                text[4] = label5.Text.ToString() + " " + comboBox1.SelectedItem.ToString();


                Directory.CreateDirectory(path + "\\" + dateTimePicker1.Text.ToString());
                File.WriteAllLines(path + "\\" + dateTimePicker1.Text.ToString() + "\\" + dateTimePicker1.Text.ToString() + ".txt", text);

            }
            else 
            {
                //Else some informations will be updated
                text = File.ReadAllLines(sfile);

                text[0] = "#Teste: " + dateTimePicker1.Text.ToString();
                text[1] = label2.Text.ToString() + " " + textBox2.Text.ToString();
                text[2] = label3.Text.ToString() + " " + textBox3.Text.ToString();
                text[3] = label4.Text.ToString() + " " + textBox4.Text.ToString();
                text[4] = label5.Text.ToString() + " " + comboBox1.SelectedItem.ToString();


                File.WriteAllLines(sfile, text);

                //Due to windows restrictions, it is necessary to check the file and folder name before creating a new one.
                if (sfile != path + "\\" + dateTimePicker1.Text.ToString() + ".txt")
                    File.Move(sfile, path + "\\" + dateTimePicker1.Text.ToString() + ".txt");

                if (path != Directory.GetParent(path).FullName + "\\" + dateTimePicker1.Text.ToString())
                    Directory.Move(path, Directory.GetParent(path).FullName + "\\" + dateTimePicker1.Text.ToString());


            }

            this.Close();

        }

        /// <summary>
        /// This function closes the form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// The following functions will swap between the text boxes using the key arrows.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                textBox2.Focus();
                e.Handled = true;
            }


        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                textBox3.Focus();
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Up)
            {
                dateTimePicker1.Focus();
                e.Handled = true;
            }
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                textBox4.Focus();
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Up)
            {
                textBox2.Focus();
                e.Handled = true;
            }
        }

        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                comboBox1.Focus();
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Up)
            {
                textBox3.Focus();
                e.Handled = true;
            }
        }

        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                button2.Focus();
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Up)
            {
                textBox4.Focus();
                e.Handled = true;
            }
        }


        /// <summary>
        /// This function will insert an image file in the test. It can be used to future image comparison.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            ///There is no restriction of file type, the user will have the option to see any file in the selected directory.
            ///But only a dicom file is a valid parameter, if it isn't the case an exception will be thrown.
            openFileDialog1.Title = "Selecionar Imagens DICOM";
            openFileDialog1.Filter = "Arquivos DICOM(*.*)|*.*";
            openFileDialog1.Multiselect = false;
            //openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();

            if (openFileDialog1.FileName == "")
                return;
            else
            {
                File.Copy(openFileDialog1.FileName, path + "\\Teste: ", true);
            }
        }
    }
}
